"""
### 异常的本质-调试错误的核心理念
### try_except基本结构
### try_多个excepts结构
### else结构
### finally结构和return语句位置
### 常见异常汇总说明
### with上下文管理资源
### traceback模块的使用-异常写入日志文件
### 自定义异常-raise抛出异常
### Pycharm调试模式debug视图
"""

# try:
#     print("step1")
#     a = 2 / 0
#     print("step2")
# except BaseException as e:
#     print("step3")
#     print(e)
#
# print("step4")

# try:
#     a = input("请输入被除数：")
#     b = input("请输入除数：")
#     c = float(a)/float(b)
#     print(c)
# except ZeroDivisionError:
#     print("异常，除数不能为0")
# except TypeError:
#     print("异常：除数和被除数都应该为数值类型")
# except BaseException as e:
#     print(e)
#     print(type(e))

# try:
#     a = input("请输入被除数：")
#     b = input("请输入除数：")
#     c = float(a)/float(b)
#     print(c)
# except BaseException as e:
#     print(e)
#     print(type(e))
# else:
#     print("除的结果为：", c)
# finally:
#     print("我是finally语句，无论是否发生异常我都会被执行")

# with open("d:/test.txt") as f:
#     context = f.readline()
#     print(context)

# import traceback
#
# try:
#     print("step1")
#     num = 1 / 0
# except:
#     traceback.print_exc()

# import traceback
#
# try:
#     print("step1")
#     num = 1 / 0
# except:
#     # 将异常信息输出到文件
#     with open("d:/test.txt", "a") as f:
#         traceback.print_exc(file=f)

# 自定义异常类
# class AgeError(Exception):
#     def __init__(self, errorinfo):
#         Exception.__init__(self)
#         self.errorinfo = errorinfo
#
#     def __str__(self):
#         return str(self.errorinfo) + ",年龄错误，应该在1-150之间"
#
#
# if __name__ == "__main__":  # 如果是true，则模块是作独立文件执行，下面可以写测试代码
#     age = int(input("输入一个年龄："))
#     if age < 1 or age > 150:
#         raise AgeError(age)
#     else:
#         print("正常的年龄：", age)

